#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "../framework/GameActivity.h"
#include "../framework/ChessBoardImp1.h"
#include "OthelloAIGreedy.h"
#include "OthelloGameFactory.h"
#include "../framework/HumanMethodAdd.h"
#include <QWidget>
#include <iostream>
#include <fstream>
#include <string>

using namespace std;

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    //new a chess game here
    game = new GameActivity(new OthelloGameFactory);
    //initialize the chess game
    game->onCreate();
    //initial chess board widget
    ui->widget->setChessBoard(game->getChessBoard());
    ui->player1Score->setSide("black");
    ui->player2Score->setSide("white");
    QObject::connect(game,SIGNAL(updateUI()),game,SLOT(notify()));
    QObject::connect(ui->widget,SIGNAL(mouseClicked(int,int)),game,SLOT(setPoint(int,int)));
    //add chessboard widget
    game->addWidget(ui->widget);
    game->addWidget(ui->player1Score);
    game->addWidget(ui->player2Score);
    cout<<"in mainwindows"<<endl;
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::on_pushButton_clicked()
{
    //start the game
    ui->pushButton->setDisabled(true);
    if(ui->radioButton->isChecked()){
        game->addPlayer("black",new HumanMethodAdd);
    }
    else{
        game->addPlayer("black",new OthelloAIGreedy);
    }
    if(ui->radioButton_3->isChecked()){
        game->addPlayer("white",new HumanMethodAdd);
    }
    else{
        game->addPlayer("white",new OthelloAIGreedy);
    }
    ui->radioButton->setDisabled(true);
    ui->radioButton_2->setDisabled(true);
    ui->radioButton_3->setDisabled(true);
    ui->radioButton_4->setDisabled(true);

    game->onStart();

}

void MainWindow::on_pushButton_2_clicked()
{
    game->onStop();
    ui->pushButton->setDisabled(false);
}

void MainWindow::on_pushButton_3_clicked()
{
    ui->radioButton->setDisabled(false);
    ui->radioButton_2->setDisabled(false);
    ui->radioButton_3->setDisabled(false);
    ui->radioButton_4->setDisabled(false);
    game->onReset();
    ui->pushButton->setDisabled(false);
}
